#ifndef     __ENCODE_H
#define     __ENCODE_H

#include <stdint.h>
#include <type_traits>

namespace utils {

inline void encode_uint8(uint8_t *buf, uint8_t val) {
    buf[0] = val;
}
inline void encode_uint16(uint8_t *buf, uint16_t val) {
    buf[0] = val >> 8;
    buf[1] = val;
}
inline void encode_uint32(uint8_t *buf, uint32_t val) {
    buf[0] = val >> 24;
    buf[1] = val >> 16;
    buf[2] = val >> 8;
    buf[3] = val;
}

template <typename T, uint32_t Size = sizeof (T)>
inline std::enable_if_t<Size == 1, void>
encode_any(uint8_t *buf, T val) {
    encode_uint8(buf, static_cast<uint8_t>(val));
}

template <typename T, uint32_t Size = sizeof (T)>
inline std::enable_if_t<Size == 2, void>
encode_any(uint8_t *buf, T val) {
    encode_uint16(buf, static_cast<uint16_t>(val));
}

template <typename T, uint32_t Size = sizeof (T)>
inline std::enable_if_t<Size == 4, void>
encode_any(uint8_t *buf, T val) {
    encode_uint32(buf, static_cast<uint32_t>(val));
}


}

#endif
